﻿using Game.DeviceHandler;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Game
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow

        [DllImport("user32.dll")]
        static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow

        [DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect); // extern method: 


        [DllImport("DmReg.dll")]
        public static extern int SetDllPathA(string dmPath, int mode);
        dmsoft dm;
   
        DeviceDm device;
        bool showState = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Common.Common.attackState = this.comboBox1.SelectedIndex;
            Common.Common.checkState = this.checkBox2.Checked;
            int hwnd = dm.FindWindow( "MapleStoryClass", "MapleStory");
            //DeviceDm device = new DeviceDm(hwnd);
            //device.Start(0);
            device = new ScriptDm(hwnd);
            device.Start(0);
            device.updateText = UpdateText;
        }


        private void UpdateText(string str)
        {
            this.txtText.Text="1";
        }
        /// <summary>
        /// 初始化大漠
        /// </summary>
        private void InitDM()
        {
            int 免注册 = SetDllPathA(Directory.GetCurrentDirectory() + "\\dm.dll", 1);
            
            if (免注册 != 1) MessageBox.Show("免注册调用失败");
            else Console.WriteLine("免注册调用成功！");
            dm = new dmsoft();

            if (dm.Ver() == "") MessageBox.Show("插件注册失败");
            else Console.WriteLine("插件注册成功 版本 " + dm.Ver());
            
            int dm_reg = dm.Reg("jack32830ac6a67c82bacebd2515d0678db64ca6f", "");
            MessageBox.Show(dm_reg.ToString());
            //int ret = dm.SetSimMode(1);
            if (dm_reg != 1)
            {
                MessageBox.Show("注册vip失败  返回错误码 :" + dm_reg.ToString());
                return;
            }
            Console.WriteLine("插件初始化成功 ");
            dm.SetPath(Directory.GetCurrentDirectory() + "\\pic");
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            InitDM();
            this.comboBox1.SelectedIndex = 0;
            timer1.Start();
            //Utility.Cmd.Instance.Regsvr32(System.Environment.CurrentDirectory + "\\lw.dll");
            // lwH = new lw.lwsoft();
        }

        private void btnSuspend_Click(object sender, EventArgs e)
        {
            //this.Location = new System.Drawing.Point(1024, 58);
            if (btnSuspend.Text == "暂停")
            {
                btnSuspend.Text = "恢复";
                device.Stop(0,false);
                return;
            }
            if (btnSuspend.Text == "恢复")
            {
                btnSuspend.Text = "暂停";
                device.Stop(0, true);
                return;
            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

           // this.textBox1
            //Time
            //dm.SetSimMode(1);
            //dm.delay(1000);
            //dm.SetWindowState(3216084, 12);
            //dm.delay(1000);
            //dm.KeyPress(13);
            //return;
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            //    MessageBox.Show(Convert.ToInt64(ts.TotalMilliseconds).ToString());
              dm.Capture(0, 0, 1024, 768, Convert.ToInt64(ts.TotalMilliseconds).ToString() + ".bmp");
            //}
        }
        protected void findRolePoint(out int x, out int y)
        {

            dm.FindPic(0, 0, 300, 150, "localTag.bmp|localTag1.bmp|localTag2.bmp|localTag3.bmp|localTag4.bmp", "050505", 0.85f, 0, out x, out y);

        }
        private void button2_Click(object sender, EventArgs e)
        {
            int hwnd = dm.FindWindow("MapleStoryClass", "MapleStory");
            dm.MoveWindow(hwnd, 0, -25);
            if (dm.GetBindWindow() <= 0)
            {
                int ret = dm.BindWindowEx(hwnd, "normal", "normal", "normal", "", 0);
            }
            int x, y;
            findRolePoint(out x, out y);
            if (this.checkBox1.Checked)
            {
                
                if(this.textBox1.TextLength == 0)
                {
                    this.textBox1.AppendText(x.ToString() + "," + y.ToString() + ",1|");
                }else
                {
                    this.textBox1.AppendText(x.ToString() + "," + y.ToString() + ",1|");
                }
            }else
            {
                if (this.textBox1.TextLength == 0)
                {
                    this.textBox1.AppendText(x.ToString() + "," + y.ToString() + ",0|");
                }
                else
                {
                    this.textBox1.AppendText(x.ToString() + "," + y.ToString() + ",0|");
                }
            }
            
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (device != null)
            {

                device.Stop1();
            }
            this.Dispose();
            this.Close();
            Application.Exit();
            
        }
        void FindAndMoveMsgBox(int x, int y, bool repaint, string title)
        {
            Thread thr = new Thread(() => // create a new thread
            {
                IntPtr msgBox = IntPtr.Zero;
                // while there's no MessageBox, FindWindow returns IntPtr.Zero
                while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;
                // after the while loop, msgBox is the handle of your MessageBox
                Rectangle r = new Rectangle();
                GetWindowRect(msgBox, out r); // Gets the rectangle of the message box
                MoveWindow(msgBox /* handle of the message box */, x, y,
                  r.Width - r.X /* width of originally message box */,
                  r.Height - r.Y /* height of originally message box */,
                  repaint /* if true, the message box repaints */);
            });
            thr.Start();
    }


        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Common.Common.checkState)
            {
                //this.txtText.AppendText(Common.Common.str);
            }
           
            if (device != null)
            {
                if (showState == false)
                {
                    
                    if (!device.getThreadState())
                    {
                        FindAndMoveMsgBox(1024, 0, true, "Title");
                        showState = true;
                        if (MessageBox.Show("出现符文", "Title", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            device.Stop(0, true);
                            showState = false;
                        }
                        else
                        {

                        }
                    }
                }
            }

        }
    }
}
